package cn.tedu.charging.order.quartz;

import cn.tedu.charging.order.Dao.repository.impl.OrderRepositoryImpl;
import cn.tedu.charging.order.pojo.po.ChargingOrderFailPO;
import cn.tedu.charging.order.pojo.po.ChargingOrderSuccessPO;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;

@Slf4j
public class DeviceCheckJob implements Job {

    public DeviceCheckJob(){
        log.debug("调用无参构造方法");
    }

    public DeviceCheckJob(String orderNo) throws SchedulerException {
        log.debug("创建设备检查任务");
        //创建调度器
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        //获取调度器
        Scheduler scheduler = schedulerFactory.getScheduler();

        long now = System.currentTimeMillis();
        long deviceCheckTime = now + 1000 * 60 ;
        Date date=new Date(deviceCheckTime);

        Trigger trigger = TriggerBuilder.newTrigger()
                .startAt(date)
                .build();

        JobDetail jobDetail = JobBuilder.newJob(DeviceCheckJob.class)
                .withIdentity("deviceCheckJob", "deviceCheckJobGroup")
                .usingJobData("orderNo", orderNo)
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.debug("任务开始执行");
        String orderNo = context.getJobDetail().getJobDataMap().getString("orderNo");
        OrderRepositoryImpl orderRepository = SpringContextUtils.getBean(OrderRepositoryImpl.class);
        log.debug("查询成功订单记录:订单号:{}", orderNo);
        ChargingOrderSuccessPO orderSuccessPO=orderRepository.getSuccessOrderByOrderNo(orderNo);
        log.debug("查询成功订单记录:订单号:{},查询结果{}",orderNo,orderSuccessPO);
        //判断是否有订单记录
        if (orderSuccessPO == null) {
            //没有成功订单记录
            log.debug("查询失败订单记录:订单号:{}",orderNo);
            ChargingOrderFailPO orderFailPO=orderRepository.getFailOrderByOrderNo(orderNo);
            log.debug("查询失败订单记录:订单号:{},查询结果{}",orderNo,orderFailPO);
            if (orderFailPO == null) {
                //没有失败订单记录
                log.debug("没有失败订单记录,说明设备<<设备无响应>>,插入失败订单记录");
                ChargingOrderFailPO chargingOrderFailPO=new ChargingOrderFailPO();
                chargingOrderFailPO.setBillId(orderNo);
                chargingOrderFailPO.setFailDesc("设备无响应");
                int row=orderRepository.insertFailOrder(chargingOrderFailPO);
                log.debug("插入失败订单记录:{},影响行数:{}",chargingOrderFailPO,row);
                log.debug("<<通知充电用户>> 更换设备");
                log.debug("<<通知充电用户>> 给用户发短信，表示歉意");
                log.debug("<<通知充电用户>> 给用户发短信，发送优惠券 营销系统，给用户心灵上的安慰");
                log.debug("<<通知设备管理员>> 对设备进行检修");
                log.debug("<<修改枪的状态为 维护中>>");

            }else{
                log.debug("有失败订单记录,说明设备<<设备有响应-无法充电>>,无需处理");

            }
        }else{
            log.debug("有成功订单记录,说明设备<<设备有响应-开始充电>>,无需处理");
        }
    }
}
